package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    void insert(Orders order);
     @Update("update orders set status = #{orderStatus},pay_status=#{orderPidStatus}, checkout_time = #{check_out_time} where number = #{orderNumber}")
    void updateStatus(Integer orderPidStatus, Integer orderStatus, LocalDateTime check_out_time, String orderNumber);

    void update(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id = #{id}")
    Orders getId(Long id);
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    @Update("update orders set status = #{status},cancel_reason = #{reason} where pay_status = 0 and order_time < #{time}")
    void processTimeoutOrder(LocalDateTime time, String reason, int status);
    @Update("update orders set status = #{deliveryInProgress} where status = 4 and pay_status = 1")
    void processDeliveryOrder(Integer deliveryInProgress, int i);

    @Select("select  from orders where user_id = #{userId} and status = 5")
    ArrayList<LocalDate> getByDate(LocalDate begin, LocalDate end);

    @Select("select sum(amount) from orders where order_time between #{localDateTimeBegin} and #{localDateTimeEnd}")
    Double sumByAmount(LocalDateTime localDateTimeBegin, LocalDateTime localDateTimeEnd);

    //动态·查询已经完成的订单在指定时间内个数
    Integer countByMap(Map map);

    /**
     * 查询商品销量排名
     * @param begin
     * @param end
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
    //动态·查询已经完成的订单在指定时间内的总金额
    Double sumByMap(Map map);
}
